<?php


namespace App\Services;

use App\Admin\Actions\Post\TransactionPost;
use App\Extensions\Actions\Upload\PlantCertUploadAction;
use Carbon\Carbon;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Tab;
use Illuminate\Database\Eloquent\Builder;

class PlantVarietyService extends BaseApplyService
{

    public function gridColumns(Grid $grid, $role = 0): Grid
    {
        $user = Admin::user();
        $grid->model()->when(request()->get('action') == 'reviewing', function ($query) {
            $query->where('review_status', 0);
        })->with(['department']);
        $grid->column('uuid', __('编号'))->modal('详情', function ($model) {
            return self::detail($model);
        });
        $grid->column('name', __('名称'));
        $grid->column('breeding_person', __('育种人'));
        $grid->column('breeder', __('育种者'));
        $grid->column('applicant', __('申请者'));
        $grid->column('level', __('登记级别'));
        $grid->column('department.name', __('dept_name'));
        $grid->column('cert_date', __('下证时间'));
        $grid->column('approved_no', __('审定编号'));

        $grid->column('cert_no', __('证书号'));
        $grid->column('publication_no', __('公告号'));
        $grid->column('cert', '审定证书')->display(function ($value) {
            return $value ? '下载' : '';
        })->link(function ($model) {
            return file_custom_preview_url($model['cert'], '(审定证书)' . $model['name']);
        });
        tree_review_status($grid);
        $grid->column('created_at', '创建时间')->display(function ($value) {
            return Carbon::parse($value)->toDateString();
        });
        $this->gridHeader($grid, $role);
        $this->gridActions($grid, $role, function (Grid\Displayers\Actions $actions, $row) use ($role, $user) {
//            if ($role == 1 && $row->review_status == 1 && !$row->status) {
//                $actions->prepend(new PlantApprovePost($actions));
//            }
//            if ($user->id == $row->user_id && !$row->cert) {
//                $actions->prepend(new PlantCertUploadAction($actions));
//            }
            if ($row->review_status == 1 && !$row->status) {
                if ($role == 1 || $user->id == $row->user_id) {
                    $actions->prepend(new PlantCertUploadAction($actions));
                }
            }
        });
        $grid->hideColumns(['cert_no', 'publication_no', 'created_at']);
        return $grid;
    }

    public function gridPublishColumns(Grid $grid, $role = 0): Grid
    {
        $grid->model()->with(['department'])->where('review_status', 1)->where('status', 1);
        $grid->column('approved_no', __('审定编号'));
        $grid->column('name', __('名称'))->modal('详情', function ($model) {
            return self::detail($model);
        });
        $grid->column('breeding_person', __('育种人'));
        $grid->column('breeder', __('育种者'));
        $grid->column('applicant', __('申请者'));
        $grid->column('level', __('登记级别'));
        $grid->column('department.name', __('dept_name'));
        $grid->column('cert_date', __('下证时间'));
        $grid->column('cert_no', __('证书编号'));
        $grid->column('publication_no', __('公告号'));
        $grid->column('cert', '审定证书')->display(function ($value) {
            return $value ? '下载' : '';
        })->link(function ($model) {
            return file_custom_preview_url($model['cert'], '(审定证书)' . $model['name']);
        });
        $grid->column('created_at', '创建时间')->display(function ($value) {
            return Carbon::parse($value)->toDateString();
        });
        $this->gridHeader($grid, $role);
        $this->gridActions($grid, $role, function (Grid\Displayers\Actions $actions, $row) use ($role) {
            if ($role == 1 && $row->status == 1) {
                $actions->prepend(new  TransactionPost($actions));
            }
        });
        $grid->disableCreateButton();
        return $grid;
    }

    protected function gridHeader(Grid $grid, $role = 0)
    {
        if ($role == 1) {
            $grid->filter(function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->column(1 / 2, function (Grid\Filter $filter) {
                    $filter->where(function ($query) {
                        $query->whereYear('created_at', "$this->input");
                    }, '申请年份')->year();
                });
                $filter->column(1 / 2, function (Grid\Filter $filter) {
                    $filter->where(function ($query) {
                        $query->whereYear('cert_date', "$this->input");
                    }, '下证年份')->year();
                });
            });
        } else {
            $grid->batchActions(function (Grid\Tools\BatchActions $actions) {
                $actions->disableDelete();
            });
            $grid->disableFilter();
        }
        $grid->disableExport();
        $grid->quickSearch(function ($query, $value) {
            $query->where(function (Builder $query) use ($value) {
                $query->where('name', 'like', "%$value%")
                    ->orWhere('applicant', 'like', "%$value%")
                    ->orWhere('breeding_person', 'like', "%$value%")
                    ->orWhere('breeder', 'like', "%$value%")
                    ->orWhere('level', 'like', "%$value%")
                    ->orWhere('approved_no', 'like', "%$value%")
                    ->orWhere('cert_no', 'like', "%$value%")
                    ->orWhereHas('department', function ($query) use ($value) {
                        $query->where('name', 'like', "%$value%");
                    });
            });
        })->placeholder('关键字');
    }

    public function formFields(Form $form, $role = 0)
    {
        $user = Admin::user();
        $form->text('name', __('拟申请植物新品种登记名称'))->required();
        $form->text('breeding_person', __('育种人姓名（全部）'))->default($user['name'])->required();
        $form->text('applicant', __('申请者'))->required()->default(org_name())->help('可以多个单位');
        $form->text('breeder', __('育种者'))->required()->default(org_name())->help('可以多个单位');
        $form->radio('level', __('登记级别'))->options(['国家级' => '国家级', '省级' => '省级'])->required();
        $form->text('approved_no', __('审定编号'));
        $form->text('cert_no', __('证书编号'));
    }

    public static function detail($model)
    {
        $tab = new Tab();
        $tab->add('基本信息', Admin::show($model, function (Show $show) use ($model) {
            $show->panel()->view('show.tab-panel');
            $show->field('uuid', __('编号'));
            $show->field('name', __('名称'));
            $show->field('breeding_person', __('育种人'));
            $show->field('applicant', __('申请者'));
            $show->field('breeder', __('育种者'));
            $show->field('level', __('登记级别'));
            $show->field('department.name', __('dept_name'));
            $show->field('cert_date', __('下证时间'));
            $show->field('approved_no', __('审定编号'));
            $show->field('cert_no', __('证书编号'));
            $show->field('publication_no', __('公告号'));
            $show->field('cert', __('审定证书'))->as(function ($val) {
                return $val ? '下载' : '';
            })->link(file_custom_preview_url($model['cert'], '(审定证书)' . $model['name']));
        }));
        return $tab;
    }
}
